{% extends "base.html" %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <a class="btn btn-outline-secondary btn-sm me-2" href="{{ request.headers.get('Referer','/resources') }}">← 返回</a>
        <h1 class="h3 mb-0 d-inline page-title">学习知识点资料汇总</h1>
    </div>
    <a class="btn btn-success" href="/resources/new">+ 添加资料</a>
</div>

<!-- 简化筛选：仅 保留 搜索关键字、所有作者、页数 -->
<form class="row g-2 mb-3 align-items-center" method="get" action="/resources">
    <div class="col-auto">
        <input class="form-control" name="q" placeholder="搜索关键字（知识点/备注）" value="{{ q or request.query_params.get('q','') }}">
    </div>
    <div class="col-auto">
        <select class="form-control" name="author_id">
            <option value="">所有作者</option>
            {% for a in authors %}
                <option value="{{ a.id }}" {% if author_id and (author_id|int) == a.id %}selected{% endif %}>{{ a.username }}</option>
            {% endfor %}
        </select>
    </div>
    <div class="col-auto">
        <input class="form-control" name="category" placeholder="类别筛选（例如：论文/教程）" value="{{ category or request.query_params.get('category','') }}">
    </div>
    <div class="col-auto">
        <select class="form-control" name="per_page">
            <option value="5" {% if per_page == 5 %}selected{% endif %}>5 / 页</option>
            <option value="10" {% if per_page == 10 %}selected{% endif %}>10 / 页</option>
            <option value="20" {% if per_page == 20 %}selected{% endif %}>20 / 页</option>
        </select>
    </div>
    <div class="col-auto">
        <button class="btn btn-outline-primary" type="submit">筛选</button>
        <a class="btn btn-link" href="/resources">重置</a>
    </div>
</form>

{% if resources %}
    <div class="table-responsive">
        <table class="table table-striped table-bordered resource-table">
            <thead>
                <tr>
                    <th class="text-center">序号</th>
                    <th class="text-center">类别</th>
                    <th class="text-center">核心知识点</th>
                    <th class="text-center">资料来源</th>
                    <th class="text-center">资料类型</th>
                    <th class="text-center">创建人</th>
                    <th class="text-center">创建时间</th>
                    <th class="text-center">附件/视频</th>
                    <th class="text-center">备注</th>
                    <th class="text-center">操作</th>
                </tr>
            </thead>
            <tbody>
            {% for r in resources %}
                <tr>
                    <td class="text-center nowrap">{{ ((page - 1) * per_page) + loop.index }}</td>
                    <td class="nowrap">{{ r.core_point }}</td>
                    <td class="nowrap">{{ r.source or '-' }}</td>
                    <td class="nowrap">{{ r.material_type or '-' }}</td>
                    <td class="nowrap">{{ r.category or '-' }}</td>
                    <td class="nowrap">{{ r.author.username if r.author else '未知' }}</td>
                    <td class="text-center nowrap">{{ r.created_at.strftime('%Y-%m-%d') if r.created_at else '' }}</td>
                    <td class="nowrap">
                        {% if r.attachments_list %}
                            {% for a in r.attachments_list %}
                                <div><a href="/uploads/{{ a.saved_name }}" target="_blank">{{ a.orig_name }}</a></div>
                                {% if a.remark %}<div class="text-muted small">备注：{{ a.remark }}</div>{% endif %}
                            {% endfor %}
                        {% endif %}
                        {% if r.video_link %}
                            <div><a href="{{ r.video_link }}" target="_blank">播放/查看 视频</a></div>
                        {% endif %}
                        {% if not r.attachments_list and not r.video_link %}-{% endif %}
                    </td>
                    <td class="nowrap">{{ r.note or '' }}</td>
                    <td class="text-center nowrap">
                        {% if user.is_admin or r.created_by == user.id %}
                            <a class="btn btn-sm btn-primary" href="/resources/{{ r.id }}/edit">编辑</a>
                        {% endif %}
                        {% if user.is_admin or r.created_by == user.id %}
                            <form method="post" action="/resources/{{ r.id }}/delete" style="display:inline;">
                                <button class="btn btn-sm btn-outline-danger" type="submit" onclick="return confirm('确定删除该条目？')">删除</button>
                            </form>
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
    <!-- 分页控件 -->
    <nav aria-label="Page navigation" class="mt-3">
        <ul class="pagination">
            {% if page > 1 %}
                            <li class="page-item">
                                <a class="page-link" href="/resources?page={{ page - 1 }}&per_page={{ per_page }}{% if q %}&q={{ q }}{% endif %}{% if source %}&source={{ source }}{% endif %}{% if material_type %}&material_type={{ material_type }}{% endif %}{% if category %}&category={{ category }}{% endif %}{% if author_id %}&author_id={{ author_id }}{% endif %}{% if created_after %}&created_after={{ created_after }}{% endif %}{% if created_before %}&created_before={{ created_before }}{% endif %}">上一页</a>
                            </li>
            {% else %}
                <li class="page-item disabled"><span class="page-link">上一页</span></li>
            {% endif %}

            {% for p in range(1, total_pages + 1) %}
                <li class="page-item {% if p == page %}active{% endif %}">
                    <a class="page-link" href="/resources?page={{ p }}&per_page={{ per_page }}{% if q %}&q={{ q }}{% endif %}{% if source %}&source={{ source }}{% endif %}{% if material_type %}&material_type={{ material_type }}{% endif %}{% if category %}&category={{ category }}{% endif %}{% if author_id %}&author_id={{ author_id }}{% endif %}{% if created_after %}&created_after={{ created_after }}{% endif %}{% if created_before %}&created_before={{ created_before }}{% endif %}">{{ p }}</a>
                </li>
            {% endfor %}

            {% if page < total_pages %}
                <li class="page-item">
                    <a class="page-link" href="/resources?page={{ page + 1 }}&per_page={{ per_page }}{% if q %}&q={{ q }}{% endif %}{% if source %}&source={{ source }}{% endif %}{% if material_type %}&material_type={{ material_type }}{% endif %}{% if category %}&category={{ category }}{% endif %}{% if author_id %}&author_id={{ author_id }}{% endif %}{% if created_after %}&created_after={{ created_after }}{% endif %}{% if created_before %}&created_before={{ created_before }}{% endif %}">下一页</a>
                </li>
            {% else %}
                <li class="page-item disabled"><span class="page-link">下一页</span></li>
            {% endif %}
        </ul>
        <small class="text-muted">共 {{ total }} 条，页次 {{ page }} / {{ total_pages }}</small>
    </nav>
{% else %}
    <div class="alert alert-info">暂无学习资料，快去添加第一条吧。</div>
{% endif %}

{% endblock %}
